//
//  LoginViewController.m
//  Address-book
//
//  Created by 任伟 on 16/1/3.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "LoginViewController.h"
#import "ContactController.h"
#import "SVProgressHUD.h"

#define kUsernameKey @"kUsernameKey"
#define kPasswordKey @"kPasswordKey"
#define kRemPwdSwitchKey @"kRemPwdSwitchKey"
#define kAutoLoginSwitchKey @"kAutoLoginSwitchKey"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UISwitch *remPwdSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 监听 文本框
    [self.usernameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [self.passwordField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
    // 监听  登录按钮的点击事件
    [self.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    // 初始化登录状态
    NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
    
    self.remPwdSwitch.on = [ud boolForKey:kRemPwdSwitchKey];
    self.autoLoginSwitch.on = [ud boolForKey:kAutoLoginSwitchKey];
    self.usernameField.text = [ud objectForKey:kUsernameKey];
    if (self.remPwdSwitch.isOn) { //如果记住密码是打开的 那么才应该显示密码
        self.passwordField.text = [ud objectForKey:kPasswordKey];
    }
    if (self.autoLoginSwitch.isOn) { // 如果自动登录是打开的  那么自动登录
        [self login];
    }
    
     [self textChange];
    
    // 获取沙盒的路径
    NSLog(@"%@",NSHomeDirectory());
}

/// 登录按钮的点击事件
- (void)login
{
    [SVProgressHUD showWithStatus:@"正在登录" maskType:SVProgressHUDMaskTypeBlack];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if([self.usernameField.text isEqualToString:@"1"] && [self.passwordField.text isEqualToString:@"1"]){
            
            // 保存数据
            NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
            
            [ud setObject:self.usernameField.text forKey:kUsernameKey];
            [ud setObject:self.passwordField.text forKey:kPasswordKey];
            [ud setBool:self.remPwdSwitch.isOn forKey:kRemPwdSwitchKey];
            [ud setBool:self.autoLoginSwitch.isOn forKey:kAutoLoginSwitchKey];
            
            // 同步
            [ud synchronize];

            
            [SVProgressHUD dismiss];
            
            // 跳转
            [self performSegueWithIdentifier:@"login2contact" sender:nil];
            
        } else {
            
            [SVProgressHUD showErrorWithStatus:@"账号或密码错误"];
            
            // 不跳转
            
        }
    });
}

// 记住密码 和自动登录的监听
// 记住密码的开关的监听
- (IBAction)remChange:(UISwitch*)sender
{
    if (!sender.isOn) {
        [self.autoLoginSwitch setOn:NO animated:YES];
    }
}

// 自动登录的开关的监听
- (IBAction)autoChange:(UISwitch*)sender
{
    if (sender.isOn) {
        [self.remPwdSwitch setOn:YES animated:YES];
    }
}


// 只要走得时sb 的线  都会调用
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    ContactController * vc = segue.destinationViewController;
    vc.username = self.usernameField.text;
}

/// 文本框值改变的时候调用
- (void)textChange {

self.loginButton.enabled = self.usernameField.text.length && self.passwordField.text.length;
    
}

@end
